package cn.yuanqiao.archive.guangpanguanli.service.impl;

import cn.yuanqiao.archive.guangpanguanli.domain.YqGptz;
import cn.yuanqiao.archive.guangpanguanli.mapper.YqGptzMapper;
import cn.yuanqiao.archive.guangpanguanli.service.IYqGptzService;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.common.core.domain.model.LoginUser;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 光盘台账Service业务层处理
 * 
 * @author sy
 * @date 2023-10-07
 */
@Service
public class YqGptzServiceImpl implements IYqGptzService
{
    @Autowired
    private YqGptzMapper yqGptzMapper;

    /**
     * 查询光盘台账
     * 
     * @param id 光盘台账主键
     * @return 光盘台账
     */
    @Override
    public YqGptz selectYqGptzById(Long id)
    {
        YqGptz yqGptz = new YqGptz();
        yqGptz.setId(id);
        return yqGptzMapper.selectYqGptzById(yqGptz);
    }

    /**
     * 查询光盘台账列表
     * 
     * @param yqGptz 光盘台账
     * @return 光盘台账
     */
    @Override
    public List<YqGptz> selectYqGptzList(YqGptz yqGptz)
    {
        return yqGptzMapper.selectYqGptzList(yqGptz);
    }

    /**
     * 导出同义词列表
     *
     * @param IDs 同义词
     * @return 同义词
     */
    @Override
    public List<YqGptz> exportYqGptzListByIDs(Long[] IDs)
    {
        YqGptz yqGptz = new YqGptz();
        return yqGptzMapper.exportYqGptzListByIDs(yqGptz.getTableName(),IDs);
    }


    /**
     * 新增光盘台账
     * 
     * @param yqGptz 光盘台账
     * @return 结果
     */
    @Override
    public String insertYqGptz(YqGptz yqGptz)
    {
        StringBuffer successMsg = new StringBuffer();
        LoginUser user = SecurityUtils.getLoginUser();

//        if(user == null){
//            user = new LoginUser();
//            user.setUserId(1L);
//        }
        yqGptz.setCreatorId(user.getUserId());
        yqGptz.setCreateTime(DateUtils.getNowDate());
        if (yqGptzMapper.insertYqGptz(yqGptz)>0){
            successMsg.append("添加成功！");
        }else {
            successMsg.append("添加失败！");
        }
        return successMsg.toString();

    }

    /**
     * 修改光盘台账
     * 
     * @param yqGptz 光盘台账
     * @return 结果
     */
    @Override
    public String updateYqGptz(YqGptz yqGptz)
    {
        StringBuffer successMsg = new StringBuffer();
        LoginUser user = SecurityUtils.getLoginUser();

//        if(user == null){
//            user = new LoginUser();
//            user.setUserId(1L);
//        }
        yqGptz.setLastupdater(user.getUserId());
        yqGptz.setLastupdateTime(DateUtils.getNowDate());
        if (yqGptzMapper.updateYqGptz(yqGptz)>0){
            successMsg.append("修改成功！");
        }else {
            successMsg.append("修改失败！");
        }
        return successMsg.toString();
    }

    /**
     * 批量删除光盘台账
     * 
     * @param ids 需要删除的光盘台账主键
     * @return 结果
     */
    @Override
    public int deleteYqGptzByIds(Long[] ids)
    {
        YqGptz yqGptz = new YqGptz();
        return yqGptzMapper.deleteYqGptzByIds(yqGptz.getTableName(),ids);
    }

    /**
     * 删除光盘台账信息
     * 
     * @param id 光盘台账主键
     * @return 结果
     */
    @Override
    public int deleteYqGptzById(Long id)
    {
        return yqGptzMapper.deleteYqGptzById(id);
    }

    @Override
    @Transactional
    public AjaxResult importYQGPTZ(List<YqGptz> gptzLists, Boolean isUpdateSupport) {
        StringBuffer msg = new StringBuffer();
        int num = 0;
        for(YqGptz yqGptz:gptzLists){
            if (StringUtils.isEmpty(yqGptz.getBurner())){
                msg.append("刻录人不能为空！");
                continue;
            }
            if (StringUtils.isEmpty(yqGptz.getBurningLocation())){
                msg.append("刻录地点不能为空！");
                continue;
            }
            if (null==yqGptz.getBurningTime()){
                msg.append("请填入合理的刻录时间！");
                continue;
            }
            if (StringUtils.isEmpty(yqGptz.getBurnContent())){
                msg.append("刻录内容不能为空！");
                continue;
            }
            Integer numObj = yqGptz.getNumber();
            if(numObj == null || numObj == 0) {
                msg.append("请填入合理的光盘张数目！");
                continue;
            } else if (yqGptz.getNumber()<=0) {
                msg.append("请填入合理的光盘张数目！");
                continue;
            }
            LoginUser user = SecurityUtils.getLoginUser();

//                if(user == null){
//                    user = new LoginUser();
//                    user.setUserId(1L);
//                }

            yqGptz.setCreatorId(user.getUserId());
            yqGptz.setCreateTime(DateUtils.getNowDate());
            num += yqGptzMapper.insertYqGptz(yqGptz);
        }
        if (num<=0){
            return AjaxResult.error("导入失败！"+msg.toString());
        }else {
            if (msg!= null && msg.length() > 0){
                return AjaxResult.success("成功"+num+"条，失败"+(gptzLists.size()-num)+"条。失败原因："+msg.toString());
            }else {
                return AjaxResult.success();
            }

        }
    }

    @Override
    public void remind(@Param("tenantCode") String tenantCode){
        String yqXxTzTable = new StringBuilder(tenantCode).append("_YQXXTZ").toString();
        String yqXxfbfwTable = new StringBuilder(tenantCode).append("_YQXXFBFW").toString();
        String msg = "请安排相关人员对“光盘台账”进行备份！";
        Date tjsj = DateUtils.getNowDate();
        String roleKey = tenantCode + "deptLeader";
        List<Long> recipientsIds = yqGptzMapper.getRecipientsIds(roleKey, tenantCode);
        if (recipientsIds.size()>0){
            yqGptzMapper.remind(yqXxTzTable,yqXxfbfwTable,SnowflakeIdGenerator.generateId(),msg,tjsj,recipientsIds);
        }
    }
}
